﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using GetServiceReference;

/// <summary>
/// Summary description for GenerateMenu
/// </summary>
public class GenerateMenu
{
	public GenerateMenu()
	{
	}

    // generate String for create menu
    public String createMenu(String naviterPage, String request)
    {
        String result = "";
        GetItemServiceClient gic = new GetItemServiceClient();
        Category[] categorys = gic.getAllCategory();
        Category[] categorysTop = categorys.Where(q => q.Category_Father.Equals(default(Guid))).ToArray();
        foreach (Category category in categorysTop)
        {
            result += generateString(categorys, category, naviterPage, request);
        }
        return result;
    }
    // nested category
    public String generateString(Category[] categorys, Category cate, String naviterPage, String request)
    {
        String result = "<li><a href='" + naviterPage + "?" + request + "=" + cate.Category_ID.ToString() + "'>" + cate.Category_Name + "</a>";
        Category[] categorysChilds = categorys.Where(q => q.Category_Father.Equals(cate.Category_ID)).ToArray();
        if (categorysChilds.Length > 0)
        {
            result += "<ul>";
            foreach (Category cateChild in categorysChilds)
            {
                result += generateString(categorys, cateChild, naviterPage, request);
            }
            result += "</ul>";
        }
        result += "</li>";
        return result;
    }
}